--[[
	The Argot marshalling routines for a book.  The TypeReader is
	handled by the TypeReaderAuto marshalling routine.
  ]]

require "argot"
require "Bookstore"
require "Book"
require "Utils"

BookArgot = { TYPENAME = argot.TypeName('book') }
BookArgot_mt = { __index = BookArgot }

function BookArgot.create(self)
	local attributes = { [[ element = argot.TypeElement( makeBoundFunction(self, self.bind) ]] }
    setmetatable( attributes, BookArgot_mt )
    return attributes
end



function BookArgot.construct(typelist, typemap)
    print('Constructing book')

    local values = {}
    local item = typelist.head

    while item do
        print('Reading item')
        obj = item_get_object(item)
        print('Inserting item')
        table.insert(values, obj)
        print('Getting next item')
        item = item.next
    end

    local isbn, title, description, author = values[1], values[2], values[3], values[4]
    
    local b = Book:create(isbn, title, description, author)
    print('constructing book done')
    
    return b
end

function BookArgot.write(out, book, element)
    print('Writing book')
    local tn = argot.u8ascii_name
    out:write_named_object(tn, book.ISBN )
    out:write_named_object(tn, book.title )
    out:write_named_object(tn, book.description )
    out:write_named_object(tn, book.author )
end